from datetime import datetime

from sqlalchemy import Column, String, Text, Boolean, Integer, ForeignKey, JSON, DateTime
from sqlalchemy.orm import relationship

from app.models.base import BaseModel


class MCPCategory(BaseModel):
    """
    MCP工具分类
    """
    name = Column(String(50), unique=True, index=True, nullable=False)
    description = Column(Text, nullable=True)
    icon = Column(String(255), nullable=True)
    
    # 关系
    tools = relationship("MCPTool", back_populates="category")
    
    def __repr__(self):
        return f"<MCPCategory {self.name}>"


class MCPTool(BaseModel):
    """
    MCP工具
    """
    name = Column(String(100), index=True, nullable=False)
    english_name = Column(String(100), unique=True, index=True, nullable=False)
    description = Column(Text, nullable=True)
    icon = Column(String(255), nullable=True)
    
    # 分类
    category_id = Column(Integer, ForeignKey("mcpcategory.id"), nullable=False)
    category = relationship("MCPCategory", back_populates="tools")
    
    # 工具配置
    api_endpoint = Column(String(255), nullable=True)
    parameters_schema = Column(JSON, nullable=True)
    mcp_config = Column(JSON, nullable=True)
    is_active = Column(Boolean, default=True)
    
    # 关系
    subscriptions = relationship("MCPSubscription", back_populates="tool")
    
    def __repr__(self):
        return f"<MCPTool {self.name}>"


class MCPSubscription(BaseModel):
    """
    MCP工具订阅/开通
    """
    user_id = Column(Integer, nullable=False, index=True)
    tool_id = Column(Integer, ForeignKey("mcptool.id"), nullable=False)
    
    is_active = Column(Boolean, default=True)
    expiry_date = Column(DateTime, nullable=True)
    
    # 关系
    tool = relationship("MCPTool", back_populates="subscriptions")
    
    def __repr__(self):
        return f"<MCPSubscription user_id={self.user_id} tool={self.tool.name if self.tool else None}>" 